#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _server;

private:
    void OnClose(const PtrConnection& connection)
    {
        DBG_LOG("close connetion > %p", connection.get());
    }

    void OnMessage(const PtrConnection& connection, Buffer* buf)
    {
        DBG_LOG("%s", buf->ReadPosition());
        buf->MoveReadOffset(buf->ReadAbleSize());
        std::string str = "algorighm!";
        connection->Send(str.c_str(), str.size());
        // connection->Shutdown();
    }

    void OnConnected(const PtrConnection& connection)
    {
        DBG_LOG("new connection > %p", connection.get());
    }
public:
    EchoServer(int port) :_server(port)  
    {
        // 创建从属线程池
        _server.SetThreadCount(2);

        // 设置回调函数
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetConectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetCloseCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
    }

    void Start()
    {
        _server.Start();
    }
};